package com.shop.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shop.common.Constants;
import com.shop.model.Category;
import com.shop.model.Goods;
import com.shop.model.User;
import com.shop.model.common.GoodsShowPage;
import com.shop.model.common.SearchPage;
import com.shop.model.common.SearchPageGoods;
import com.shop.service.CategoryService;
import com.shop.service.GoodsService;
import com.shop.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/add")
    public ModelAndView goodsAdd(){
        ModelAndView modelAndView=new ModelAndView();
        List<Category> categoryList = categoryService.selectAllCategory();
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("admin/goods_add");
        return modelAndView;
    }

    @PostMapping("/add")
    public ModelAndView goodsAdding(@RequestParam Map<String, Object> params,
                                    RedirectAttributes redirectAttributes){
        log.info("map:{}",params);
        goodsService.goodsAdd(params);
        return new ModelAndView("redirect:/goods/add");
    }


    @GetMapping("/showPage")
    public ModelAndView modelAndView(@RequestParam Map map){
        ModelAndView modelAndView=new ModelAndView();
        log.info("map:{}",map);
        List<Integer> category;
        if(map.get("categoryId")!=null&&!"".equals(map.get("categoryId"))) {
            category =categoryService.selectCategoryIdByPid(Integer.parseInt(map.get("categoryId").toString()));
            category.add(Integer.parseInt(map.get("categoryId").toString()));
        } else {
            category=null;
        }
        map.remove("categoryId");
        map.put("categoryId",category);
        ObjectMapper mapper=new ObjectMapper();
        GoodsShowPage goodsShowPage=mapper.convertValue(map, GoodsShowPage.class);
        if(goodsShowPage.getPage()==null) goodsShowPage.setPage(1);
        goodsShowPage.setPageSize(14);
        if("".equals(goodsShowPage.getName())) goodsShowPage.setName(null);
        if("".equals(goodsShowPage.getStatus())) goodsShowPage.setStatus(null);

        List<Goods> goodsList=goodsService.showPage(goodsShowPage);
        Integer count=goodsService.showPageCount(goodsShowPage);
        Integer totalPages=count/goodsShowPage.getPageSize()+1;
        if(count%goodsShowPage.getPageSize()==0) totalPages=count/goodsShowPage.getPageSize();
        List<Category> categoryList=categoryService.selectAllCategory();
        modelAndView.addObject("categoryList",categoryList);
        modelAndView.addObject("goodsList", goodsList);
        modelAndView.addObject("pageTotal",totalPages);
        modelAndView.addObject("count",count);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @GetMapping("/{id}")
    public ModelAndView getGoodsById(@PathVariable Integer id){
        ModelAndView modelAndView=new ModelAndView();
        Goods goods=goodsService.selectById(id);
        modelAndView.addObject("goods",goods);
        modelAndView.setViewName("admin/goods_view");
        return  modelAndView;
    }

}
